<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件
function curl_func($url,$data='',$method='GET',$encode='UTF-8',$ssl='',$phpsess='')
{
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_TIMEOUT, 1);
    curl_setopt($curl, CURLOPT_HEADER, 0);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);   //1不显示内容，0显示内容
    
    if ($ssl = 'ssl') {
    	curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    }
    if ($method == 'POST') {
    	curl_setopt($curl, CURLOPT_POST, 1);
    	curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    }else{
        $header=array(
            "cookie:PHPSESSID=".$phpsess
            // "Accept: application/json",
            // "Content-Type: application/json;charset=utf-8",
        );
        curl_setopt($curl, CURLOPT_HTTPHEADER, $header); 
    }
    $data = curl_exec($curl);
    curl_close($curl);
    if ($encode == 'GBK') {
    	$data = mb_convert_encoding($data, 'utf-8', 'GBK,UTF-8,ASCII');
    }
    return $data;
}
/**
 * Json返回
 * @param string $message
 * @param int $code 0 默认成功
 * @return string
 */
function api_return($message='', $code='', $count=0 , $data=[])
{
    $return = [
        'code' => $code ?: 0,
        'msg'  => $message ?: 'success',
        'count' => $count ?:0,
        'data' => $data
    ];
    return json($return);
};



//支付密码及登录密码  hash加密
function password($password){
    if (!isset($password)) {echo json_encode(array('code'=>2301,'msg'=>'密码不能为空！','data'=>""));exit;}
    $password = trim($password);
    if (empty($password)) {echo json_encode(array('code'=>2301,'msg'=>'密码不能为空！','data'=>""));exit;}
    $password = password_hash($password, PASSWORD_DEFAULT);
    return $password;
}


function id_card($id_card){
    // $id_card = '360822197801180255';
    $z       = substr($id_card,17);
    $xishu   = array(7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2);
    $a = substr($id_card,0,17);
    $b = str_split($a);
    $c = null;$d = null;
    foreach ($xishu as $key => $value) {$c[] = $b[$key]*$value;}
    foreach ($c as $key) {$d = $d + $key;}
    $e = $d % 11 ;
    switch($e){ 
    case 0: $e = 1;break; 
    case 1: $e = 0;break; 
    case 2: $e = "X";break; 
    case 3: $e = 9;break; 
    case 4: $e = 8;break; 
    case 5: $e = 7;break; 
    case 6: $e = 6;break; 
    case 7: $e = 5;break; 
    case 8: $e = 4;break; 
    case 9: $e = 3;break; 
    case 10: $e = 2;break; 
    default: return 3; 
    } 
    if ($e == $z) {
        return 1;
    }else{
        return 2;
    }
}